<?php

namespace Cms;

class Ads extends \Model {
    
    use \State;

    static public $table = 'cms_ads';
    
    protected $_fillable = [
        'position' => 'str',
        'sort' => 'int',
        'state' => 'int',
        'title' => 'str',
        'image' => 'str',
        'url' => 'str',
    ];
    
    const STATE_ON = 1;
    const STATE_OFF = -1;
    
    protected $_states = [
        self::STATE_ON => '显示',
        self::STATE_OFF => '隐藏',
    ];

    public function getList() {
        $where = array('state' => 1);
        return Db()->getAll(static::$table)->where($where)->order('sort desc, id desc')->result();
    }
    
    const POSITION_HOME_2COL = 'home_2col';
    const POSITION_HOME_LARGE = 'home_large';
    
    public function getPositionAssoc($key = null) {
        $arr = array(
            self::POSITION_HOME_2COL => '首页2栏',
            self::POSITION_HOME_LARGE => '首页贯穿',
        );
        
        if ($key !== null) {
            if (array_key_exists(strval($key), $arr)) {
                return $arr[strval($key)];
            } else {
                return '';
            }
        } else {
            return $arr;
        }
    }
    
    public function getByPosition($position) {
        $where = array('position' => $position, 'state' => 1);
        $result = Db()->getRow(static::$table)->where($where)->result();
        return $result;
    }

    public function getListByPosition($position) {
        $where = array('position' => $position, 'state' => 1);
        $result = Db()->getAll(static::$table)->where($where)->result();
        return $result;
    }

}
